-module(counter).
-export([start/1,add/1,value/1]).
-export([init/1]).

start(InitCount) ->
    spawn_link(?MODULE, init, [InitCount]).

add(Name) -> call({add, Name}).
value(Name) -> call({value, Name}).

call({Request, Name}) ->
    Name ! {request, Request, self()},
    receive
	{reply, Reply} -> Reply
    end.

init(Count) ->
    receive
	{request, add, From} ->
	    reply(From, Count+1),
	    init(Count+1);
	 {request, value, From} ->
	    reply(From, Count),
	    init(Count)
    end.

reply(From, Count) ->
    From ! {reply, Count}.
